<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<header th:replace="admin/header::header-fragment">
</header>
<body>
<shiro:user>
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-header">网站设置</div>
                <div class="layui-card-body" pad15>

                    <div class="layui-form" wid100 lay-filter="">
                        <div class="layui-form-item">
                            <div class="layui-inline">
                                <label class="layui-form-label">文章标题</label>
                                <div class="layui-input-inline">
                                    <input type="text" name="title" lay-verify="required" placeholder="请输入文章标题" autocomplete="off" class="layui-input">
                                </div>
                            </div>
                            <div class="layui-inline">
                                <label class="layui-form-label">文章分类</label>
                                <div class="layui-input-inline">
                                    <select name="modules" lay-verify="required" lay-search="" id="blogText">
                                        <option th:each="blogclass:${blogClassifies}" th:value="${blogclass.classifyId}" th:text="${blogclass.classifyName}"></option>
                                    </select>
                                </div>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <fieldset class="layui-elem-field layui-field-title" style="margin-top: 50px;">
                                <legend>文章内容</legend>
                            </fieldset>
                            <textarea class="layui-textarea" id="LAY_demo1" name="LAY_demo1" style="display: none"></textarea>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">发布状态</label>
                            <div class="layui-input-inline">
                                <input type="checkbox" lay-verify="required" lay-filter="status" id="status" name="status" lay-skin="switch" lay-text="已发布|待修改" value="1">
                            </div>
                        </div>
                        <div class="layui-input-block">
                            <button class="layui-btn" lay-submit lay-filter="layuiadmin-app-form-submit">确认保存</button>
                        </div>
                    </div>
                </div>

            </div>
        </div>
    </div>
</div>
    </shiro:user>
<shiro:guest>
    <footer th:replace="admin/nologin::header-fragment">
    </footer>
</shiro:guest>
<script src="/layuiadmin/layui/layui.js"></script>
<script>
    layui.config({
        base: 'layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'form','layedit','element'], function(){
        var $ = layui.$
            ,form = layui.form
            ,layedit = layui.layedit
            ,$ = layui.jquery,
            element = layui.element;
        form.render();
        //上传图片,必须放在 创建一个编辑器前面
        layedit.set({
            uploadImage: {
                url: 'admin/uploadPicture' //接口url
                ,type: 'post' //默认post
            }
        });
        //构建一个默认的编辑器
        var index2 = layedit.build('LAY_demo1');

        //编辑器外部操作
        var active = {
            content: function(){
                alert(layedit.getContent(index)); //获取编辑器内容
            }
            ,text: function(){
                alert(layedit.getText(index)); //获取编辑器纯文本内容
            }
            ,selection: function(){
                alert(layedit.getSelection(index));
            }
        };

        $('.site-demo-layedit').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
        //监听提交
        form.on('submit(layuiadmin-app-form-submit)', function(data){
            var field = data.field; //获取提交的字段
            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
            var sta = field.status==1?1:0;  //获取状态
            var content = layedit.getContent(index2);
            var blogText = $("#blogText").find("option:selected").text()
            //提交 Ajax 成功后，关闭当前弹层并重载表格
            $.post('admin/addBlog',{
                "title":field.title,
                "modules":field.modules,
                "status":sta,
                "content":content,
                "blogText":blogText
            },function (res){
                if (res.success){
                    if (sta==1){
                        layer.msg('发布文章成功', {
                            offset: '15px'
                            ,icon: 1
                            ,time: 1000
                        });
                    }else {
                        layer.msg('保存草稿成功', {
                            offset: '15px'
                            ,icon: 1
                            ,time: 1000
                        });
                    }

                    $("input[name='title']").val('');
                    layui.layedit.clearContent(index)
                }else {
                    layer.msg('发布失败', {
                        offset: '15px'
                        ,icon: 1
                        ,time: 1000
                    });
                }
            },'json');
        });
    })
</script>

</body>
</html>